//链栈实现
#include<stdio.h>
#include<malloc.h>

typedef struct StackNode
{
    int data;
    struct StackNode *next;
}StackNode,*LinkStack;

int main()
{
    LinkStack head;
    head=(LinkStack)malloc(sizeof(StackNode));
    head->next=NULL;

    int Push(LinkStack top,int e);
    int Pop(LinkStack head);
    int GetTop(LinkStack head);

    int type,e;
    printf("---------------功能菜单---------------\n");
    printf("【1】入栈   【2】出栈   【3】取栈顶元素\n");
    printf("--------------------------------------\n");
    printf("请输入你想要的功能：");
    scanf("%d",&type);
    while(type!=-1)
    {
        switch(type)
        {
            case 1:
            {
                printf("请输入需要入栈的元素：");
                scanf("%d",&e);
                Push(head,e);
                printf("请输入你想要的功能：");
                break;
            }
            case 2:
            {
                e=Pop(head);
                printf("元素出栈成功：%d\n",e);
                printf("请输入你想要的功能：");
                break;
            }
            case 3:
            {
                e=GetTop(head);
                printf("栈顶元素的值：%d\n",e);
                printf("请输入你想要的功能：");
                break;
            }
        }
        scanf("%d",&type);
    }
}

int Push(LinkStack head,int e)
{
    LinkStack p;
    p=(LinkStack)malloc(sizeof(StackNode));
    p->data=e;
    p->next=head->next;
    head->next=p;
}

int Pop(LinkStack head)
{
    int e;
    LinkStack p;
    p=head->next;
    e=p->data;
    head->next=p->next;
    return(e);
}

int GetTop(LinkStack head)
{
    int e;
    e=head->next->data;
    return(e);
}